﻿using System;
using System.Data;
using System.Collections.Generic;
using Model;
namespace BLL
{
    /// <summary>
    /// PMS_RoleList
    /// </summary>
    public class PMS_RoleListBLL
    {
        private readonly DAL.PMS_RoleListDAL dal = new DAL.PMS_RoleListDAL();
        public PMS_RoleListBLL()
        { }

        /// <summary>
        /// 单条数据
        /// </summary>
        /// <param name="roleID"></param>
        /// <returns></returns>
        public PMS_RoleList GetModel(int roleID)
        {
            return dal.GetModel(roleID);
        }
        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <returns></returns>
        public List<PMS_RoleList> GetList()
        {
            return dal.GetList("");
        }

        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <returns></returns>
        public List<PMS_RoleList> GetList(string parentID)
        {
            return dal.GetList(" parentID='" + parentID + "' order by roleValue");
        }

        /// <summary>
        /// 查询用户数据
        /// </summary>
        /// <param name="roleID"></param>
        /// <returns></returns>
        public List<PMS_RoleList> GetList(int roleID)
        {
            PMS_RoleList model = dal.GetModel(roleID);
            string roles = roleID.ToString();
            string roleIDs = "'" + roleID + "'";
            if (model.parentID != 0)
            {
                roles = model.roleIDs;
                string[] ls = model.roleIDs.Split('|');
                foreach (string s in ls)
                {
                    roleIDs += ",'" + s + "'";
                }
            }
            string strWhere = "roleID in(" + roleIDs + ") or roleIDs='" + roles + "' or roleIDs like '" + roles + "|%'";
            return dal.GetList(strWhere);
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool AddUpdate(PMS_RoleList model)
        {
            if (model.roleID <= 0)
            {
                return dal.Add(model);
            }
            else
            {
                return dal.Update(model);
            }
        }

        /// <summary>
        /// 菜单是否存在
        /// </summary>
        /// <param name="loginName"></param>
        /// <returns></returns>
        public bool Exite(string roleText)
        {
            List<PMS_RoleList> list = dal.GetList("roleText='" + roleText + "'");
            if (list != null && list.Count > 0)
            {
                return true;
            }
            else
                return false;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="roleID"></param>
        /// <returns></returns>
        public bool Delete(int roleID)
        {
            return dal.Delete(roleID);
        }
    }
}

